package com.lovo.system.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.system.dto.SupplierDto;
import com.lovo.system.mapper.SupplierMapper;
import com.lovo.system.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierDto> implements ISupplierService {

    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public IPage<SupplierDto> cutByItem(int curPage, int pageSize, String name, String range) {
        QueryWrapper qw = new QueryWrapper();
        if (name != null && !"".equals(name)){
            qw.like("supplier_name",name);
        }
        if (range != null && !"".equals(range)){
            qw.like("nature_of_business",range);
        }
        return supplierMapper.selectPage(new Page<>(curPage,pageSize),qw);
    }

    @Override
    public String addSupplier(SupplierDto supplierDto) {
        try {
            supplierMapper.insert(supplierDto);
            return "添加成功";
        } catch (Exception e) {
            return "添加失败";
        }
    }

    @Override
    public String deleteSupplier(int id) {
        try {
            supplierMapper.deleteById(id);
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }

    @Override
    public String updateStudent(SupplierDto supplierDto) {
        try {
            supplierMapper.updateById(supplierDto);
            return "修改成功";
        } catch (Exception e) {
            return "修改失败";
        }
    }

    @Override
    public SupplierDto queryOneById(int id) {
        return supplierMapper.selectById(id);
    }


    @Override
    public void importSupplierList(MultipartFile file) throws Exception {
        //输入流
        InputStream inputStream = file.getInputStream();

        //importparams参数设置
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        params.setStartRows(0);
        params.setStartSheetIndex(0);
        params.setVerifyHandler(null);

        //ExcelImportUtil工具
        List<SupplierDto> list = ExcelImportUtil.importExcel(inputStream, SupplierDto.class, params);

        if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isEmpty(list)) {
            log.error("导入信息不能为空");
        }

        //遍历list集合写入数据
        list.stream().forEach(supplierDto -> {
            supplierMapper.insert(supplierDto);
        });
    }


}
